home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / book / Chap17 / GLView / GLViewDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-20  |  8.2 KB  |  322 lines

  1. // GLViewDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "GLView.h"
  6. #include "GLViewDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16.  
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20.     CAboutDlg();
  21.  
  22. // Dialog Data
  23.     //{{AFX_DATA(CAboutDlg)
  24.     enum { IDD = IDD_ABOUTBOX };
  25.     //}}AFX_DATA
  26.  
  27.     // ClassWizard generated virtual function overrides
  28.     //{{AFX_VIRTUAL(CAboutDlg)
  29.     protected:
  30.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  31.     //}}AFX_VIRTUAL
  32.  
  33. // Implementation
  34. protected:
  35.     //{{AFX_MSG(CAboutDlg)
  36.     //}}AFX_MSG
  37.     DECLARE_MESSAGE_MAP()
  38. };
  39.  
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42.     //{{AFX_DATA_INIT(CAboutDlg)
  43.     //}}AFX_DATA_INIT
  44. }
  45.  
  46. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48.     CDialog::DoDataExchange(pDX);
  49.     //{{AFX_DATA_MAP(CAboutDlg)
  50.     //}}AFX_DATA_MAP
  51. }
  52.  
  53. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  54.     //{{AFX_MSG_MAP(CAboutDlg)
  55.         // No message handlers
  56.     //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CGLViewDlg dialog
  61.  
  62. CGLViewDlg::CGLViewDlg(CWnd* pParent /*=NULL*/)
  63.     : CDialog(CGLViewDlg::IDD, pParent)
  64. {
  65.     //{{AFX_DATA_INIT(CGLViewDlg)
  66.         // NOTE: the ClassWizard will add member initialization here
  67.     //}}AFX_DATA_INIT
  68.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  69.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  70. }
  71.  
  72. void CGLViewDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74.     CDialog::DoDataExchange(pDX);
  75.     //{{AFX_DATA_MAP(CGLViewDlg)
  76.     DDX_Control(pDX, IDC_LST_EXT, m_lbGLExtensions);
  77.     DDX_Control(pDX, IDC_LIST, m_ListBox);
  78.     //}}AFX_DATA_MAP
  79. }
  80.  
  81. BEGIN_MESSAGE_MAP(CGLViewDlg, CDialog)
  82.     //{{AFX_MSG_MAP(CGLViewDlg)
  83.     ON_WM_SYSCOMMAND()
  84.     ON_WM_PAINT()
  85.     ON_WM_QUERYDRAGICON()
  86.     ON_LBN_SELCHANGE(IDC_LIST, OnSelchangeList)
  87.     //}}AFX_MSG_MAP
  88. END_MESSAGE_MAP()
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CGLViewDlg message handlers
  92.  
  93. BOOL CGLViewDlg::OnInitDialog()
  94.     {
  95.     PIXELFORMATDESCRIPTOR pfd;        // Pixel format descriptor
  96.     char cBuffer[64];                // String buffer 
  97.     char cOutput[128];
  98.     int nTabs[] = { 16, 40, 64, 95, 159, 195, 222 };
  99.     
  100.     CDialog::OnInitDialog();
  101.     m_ListBox.SetTabStops(7, nTabs);
  102.  
  103.     // Add "About..." menu item to system menu.
  104.  
  105.     // IDM_ABOUTBOX must be in the system command range.
  106.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  107.     ASSERT(IDM_ABOUTBOX < 0xF000);
  108.  
  109.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  110.     if (pSysMenu != NULL)
  111.         {
  112.         CString strAboutMenu;
  113.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  114.         if (!strAboutMenu.IsEmpty())
  115.             {
  116.             pSysMenu->AppendMenu(MF_SEPARATOR);
  117.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  118.             }
  119.         }
  120.  
  121.     // Set the icon for this dialog.  The framework does this automatically
  122.     //  when the application's main window is not a dialog
  123.     SetIcon(m_hIcon, TRUE);            // Set big icon
  124.     SetIcon(m_hIcon, FALSE);        // Set small icon
  125.     
  126.     WNDCLASS wndClass;
  127.  
  128.     wndClass.style = CS_OWNDC;
  129.     wndClass.lpfnWndProc = AfxWndProc;
  130.     wndClass.cbClsExtra = 0;
  131.     wndClass.cbWndExtra = 0;
  132.     wndClass.hInstance = AfxGetInstanceHandle();
  133.     wndClass.hIcon = NULL;
  134.     wndClass.hCursor = NULL;
  135.     wndClass.hbrBackground = NULL;
  136.     wndClass.lpszMenuName = NULL;
  137.     wndClass.lpszClassName = "OpenGLClass";
  138.  
  139.     // Register the window class
  140.     AfxRegisterClass(&wndClass);
  141.  
  142.  
  143.     // Get the number of pixel formats
  144.     // Will need a device context
  145.     pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  146.     HDC hDC = ::GetDC(this->m_hWnd);
  147.     int nFormatCount = DescribePixelFormat(hDC, 1, 0, NULL);
  148.  
  149.     // Retrieve each pixel format and store a pointer to it in the list box
  150.     // Parse it apart and put some relevant data in the list box columns
  151.     for(int i = 1; i <= nFormatCount; i++)
  152.         {
  153.         // Get description of pixel format
  154.         DescribePixelFormat(hDC, i, pfd.nSize, &pfd);
  155.         
  156.         // Pixel format identifier
  157.         wsprintf(cOutput,"%d\t",i);
  158.  
  159.         // Driver type /////////////////////////////////
  160.         if(!(pfd.dwFlags & PFD_GENERIC_FORMAT))
  161.             strcat(cOutput,"ICD\t");
  162.         else
  163.             {
  164.             if(pfd.dwFlags & PFD_GENERIC_ACCELERATED)
  165.                 strcat(cOutput,"MCD\t");
  166.             else
  167.                 strcat(cOutput,"GEN\t");
  168.             }
  169.  
  170.         
  171.         // Double or single buffer
  172.         if(pfd.dwFlags & PFD_DOUBLEBUFFER)
  173.             strcat(cOutput,"DBL\t");
  174.         else
  175.             strcat(cOutput,"SGL\t");
  176.  
  177.         // RGBA or CI
  178.         if(pfd.iPixelType == PFD_TYPE_RGBA)
  179.             strcat(cOutput,"RGBA\t");
  180.         else
  181.             strcat(cOutput,"CI\t");
  182.  
  183.  
  184.         // Color
  185.         wsprintf(cBuffer,"%d (%d-%d-%d-%d)\t",pfd.cColorBits, pfd.cRedBits, 
  186.             pfd.cGreenBits, pfd.cBlueBits, pfd.cAlphaBits);
  187.         strcat(cOutput, cBuffer);
  188.  
  189.         // Depth, alpha, stencil, Accumulation
  190.         wsprintf(cBuffer,"%d\t%d\t%d(%d-%d-%d-%d)",pfd.cDepthBits,
  191.             pfd.cStencilBits,pfd.cAccumBits,
  192.             pfd.cAccumRedBits, pfd.cAccumGreenBits, pfd.cAccumBlueBits,
  193.             pfd.cAccumAlphaBits);
  194.         strcat(cOutput, cBuffer);
  195.  
  196.         // Finally, add to list box
  197.         m_ListBox.AddString(cOutput);
  198.         }
  199.  
  200.  
  201.     m_ListBox.SetCurSel(0);
  202.  
  203.     // Free the Device Context
  204.     ::ReleaseDC(this->m_hWnd, hDC);
  205.  
  206.  
  207.     CRect glRect;
  208.     CRect dlgRect;
  209.     CRect clRect;
  210.  
  211.     GetWindowRect(&dlgRect);
  212.     GetClientRect(&clRect);
  213.  
  214.     // Create the storm direction window control
  215.     GetDlgItem(IDC_TEST_FRAME)->GetWindowRect(&glRect);
  216.  
  217.     newRect.top = glRect.top - dlgRect.top - (dlgRect.Height() - clRect.Height()) +20;
  218.     newRect.bottom = glRect.bottom - dlgRect.top - (dlgRect.Height() - clRect.Height()) -3;
  219.     newRect.left = glRect.left - dlgRect.left - (dlgRect.Width() - clRect.Width()) +9;
  220.     newRect.right = glRect.right - dlgRect.left  - (dlgRect.Width() - clRect.Width()) -1 ;
  221.  
  222.  
  223.     // Create initial window
  224.     OnSelchangeList();
  225.  
  226.     return TRUE;  // return TRUE  unless you set the focus to a control
  227.     }
  228.  
  229. void CGLViewDlg::OnSysCommand(UINT nID, LPARAM lParam)
  230.     {
  231.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  232.         {
  233.         CAboutDlg dlgAbout;
  234.         dlgAbout.DoModal();
  235.         }
  236.     else
  237.         {
  238.         CDialog::OnSysCommand(nID, lParam);
  239.         }
  240.     }    
  241.  
  242. // If you add a minimize button to your dialog, you will need the code below
  243. //  to draw the icon.  For MFC applications using the document/view model,
  244. //  this is automatically done for you by the framework.
  245.  
  246. void CGLViewDlg::OnPaint() 
  247.     {
  248.     if (IsIconic())
  249.         {
  250.         CPaintDC dc(this); // device context for painting
  251.  
  252.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  253.  
  254.         // Center icon in client rectangle
  255.         int cxIcon = GetSystemMetrics(SM_CXICON);
  256.         int cyIcon = GetSystemMetrics(SM_CYICON);
  257.         CRect rect;
  258.         GetClientRect(&rect);
  259.         int x = (rect.Width() - cxIcon + 1) / 2;
  260.         int y = (rect.Height() - cyIcon + 1) / 2;
  261.  
  262.         // Draw the icon
  263.         dc.DrawIcon(x, y, m_hIcon);
  264.         }
  265.     else
  266.         {
  267.         CDialog::OnPaint();
  268.         }
  269.     }
  270.  
  271. // The system calls this to obtain the cursor to display while the user drags
  272. //  the minimized window.
  273. HCURSOR CGLViewDlg::OnQueryDragIcon()
  274.     {
  275.     return (HCURSOR) m_hIcon;
  276.     }
  277.  
  278. // Whenever the selection in the list box changes, we must destroy the current
  279. // window if it exists, and create a new one
  280. void CGLViewDlg::OnSelchangeList() 
  281.     {
  282.     // Destroy the window and reset the pixel format
  283.     m_glWnd.DestroyWindow();
  284.     m_glWnd.m_nPixelFormat = m_ListBox.GetCurSel() + 1;
  285.  
  286.     m_glWnd.Create("OpenGLClass",NULL,WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  287.             newRect,this,901,NULL);
  288.     
  289.     m_glWnd.Invalidate(FALSE);
  290.  
  291.     // Update String info
  292.     SetDlgItemText(IDC_VENDOR,(const char *)glGetString(GL_VENDOR));
  293.     SetDlgItemText(IDC_RENDERER, (const char *)glGetString(GL_RENDERER));
  294.     SetDlgItemText(IDC_VERSION, (const char *)glGetString(GL_VERSION));
  295.  
  296.     // Display GLU Driver/Implementation info.
  297.     SetDlgItemText(IDC_GLUVERSION, (const char *)gluGetString(GLU_VERSION));
  298.  
  299.     //////////////////////////////////////////////////////////////////////
  300.     // Put all GL Extensions in the list box
  301.     char *szTemp;
  302.     char *szStringTemp;
  303.     m_lbGLExtensions.ResetContent();
  304.     if(glGetString(GL_EXTENSIONS) != NULL)
  305.         {
  306.         szStringTemp = strdup((const char *)glGetString(GL_EXTENSIONS));
  307.  
  308.         szTemp = strtok(szStringTemp," ");
  309.         while(szTemp != NULL)
  310.             {
  311.             m_lbGLExtensions.AddString(szTemp);
  312.             szTemp = strtok(NULL," ");
  313.             }
  314.  
  315.         free(szStringTemp);
  316.         }
  317.  
  318.  
  319.     // Update Extensions list
  320.  
  321.     }
  322.